/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.helper;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Evgeniy Grigorevskiy
 */
public class Coockies {
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Map coockiesCode = new HashMap<String, String>();
    private int timeToLive = 14 * 24 * 60 * 60; //14 days
    private Cookie userLoginCookie = null;
    private Cookie userPassCookie = null;

    public void setTimeToLive(int timeToLive) {
        this.timeToLive = timeToLive;
    }

    public int getTimeToLive() {
        return timeToLive;
    }

    public Coockies() {
        coockiesCode.put("userLogin", "saveParamsOne");
        coockiesCode.put("userPass", "saveParamsTwo");
    }
    public Coockies(ServletRequest req, ServletResponse resp) {
        coockiesCode.put("userLogin", "saveParamsOne");
        coockiesCode.put("userPass", "saveParamsTwo");
        this.request = (HttpServletRequest) req;
        this.response = (HttpServletResponse) resp;
    }
    public void createCoockies(String userName, String password) {
        Cookie cookieLog = new Cookie(coockiesCode.get("userLogin").toString(), userName);
        cookieLog.setMaxAge(timeToLive);
        Cookie cookiePas = new Cookie(coockiesCode.get("userPass").toString(), password);
        cookiePas.setMaxAge(timeToLive);
        
        response.addCookie(cookieLog);
        response.addCookie(cookiePas);
    }

    public boolean checkIfExistCoockies(ServletRequest req, ServletResponse resp) {
        this.request = (HttpServletRequest) req;
        this.response = (HttpServletResponse) resp;
        Cookie cookies[] = request.getCookies();
        
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(coockiesCode.get("userLogin").toString())) {
                    userLoginCookie = cookies[i];
                }
                if (cookies[i].getName().equals(coockiesCode.get("userPass").toString())) {
                    userPassCookie = cookies[i];
                }
            }
            if (userLoginCookie!=null && userPassCookie!=null){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    public boolean checkIfExistCoockies() {
        Cookie cookies[] = request.getCookies();
        
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(coockiesCode.get("userLogin").toString())) {
                    userLoginCookie = cookies[i];
                }
                if (cookies[i].getName().equals(coockiesCode.get("userPass").toString())) {
                    userPassCookie = cookies[i];
                }
            }
            if (userLoginCookie!=null && userPassCookie!=null){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    public void updateCoockies() {
        createCoockies(userLoginCookie.getValue().toString(),userPassCookie.getValue().toString());
    }
    
    public void deleteCoockies(){
        Cookie cookieLog = new Cookie(coockiesCode.get("userLogin").toString(), "toDelete");
        cookieLog.setMaxAge(0);
        Cookie cookiePas = new Cookie(coockiesCode.get("userPass").toString(), "toDelete");
        cookiePas.setMaxAge(0);
        
        response.addCookie(cookieLog);
        response.addCookie(cookiePas);
    }
    
    public Map getInfoFromCoockies(){
        Map userInfo = new HashMap<String,String>();
        userInfo.put("userLogin", userLoginCookie.getValue().toString());
        userInfo.put("userPass", userPassCookie.getValue().toString());
        return userInfo;
    }
}
